{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import urllib\n",
    "import math\n",
    "import time\n",
    "import random\n",
    "import networkx as nx\n",
    "import copy\n",
    "from bidict import bidict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = nx.watts_strogatz_graph(100000, 35, 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = nx.Graph()\n",
    "f = open(\"C:/Users/user/Desktop/datasets/ws.txt\",\"r\")\n",
    "for line in f:\n",
    "    srcTar = line.strip().split()\n",
    "    src = long(srcTar[0])\n",
    "    tar = long(srcTar[1])\n",
    "#     print src, tar\n",
    "    G.add_edge(src,tar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1700000 100000\n"
     ]
    }
   ],
   "source": [
    "edges = list(G.edges)\n",
    "nodes = list(G.nodes)\n",
    "print len(edges), len(nodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = list(nx.algorithms.coloring.strategy_connected_sequential_dfs(G, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65532\n"
     ]
    }
   ],
   "source": [
    "print len(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "cc = {}\n",
    "for i in range(len(c)):\n",
    "    cc[c[i]] = i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "sortedlist = sorted(edges, key = lambda x: (cc[x[0]], cc[x[1]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn = open(\"C:/Users/user/Desktop/datasets/rmat-dfs-edges.txt\",\"w+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(sortedlist)):\n",
    "    s = str(sortedlist[i][0]) + '\\t' + str(sortedlist[i][1]) + '\\n'\n",
    "    fn.write(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "fnn = open(\"C:/Users/user/Desktop/datasets/rmat-dfs-nodes.txt\",\"w+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(c)):\n",
    "    s = str(c[i]) + '\\n'\n",
    "    fnn.write(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "fnn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(edges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "fr = open(\"C:/Users/user/Desktop/datasets/rmat-rand.txt\",\"w+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(edges)):\n",
    "    s = str(edges[i][0]) + '\\t' + str(edges[i][1]) + '\\n'\n",
    "    fr.write(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "fr.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
